package com.collect.discord.web.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.collect.common.eventbus.annotation.EventBusListenerAnnotation;
import com.collect.common.eventbus.enums.EventBusMode;
import com.collect.common.eventbus.message.EventBusMessage;
import com.collect.common.utils.eventbus.EventBusUtils;
import com.collect.common.utils.sensitive.SensitiveWordUtils;
import com.collect.discord.web.domain.DiscordMessage;
import com.collect.discord.web.mapper.DiscordMessageMapper;
import com.collect.discord.web.service.IDiscordMessageService;
import com.google.common.eventbus.Subscribe;
import net.dv8tion.jda.api.OnlineStatus;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.*;
import net.dv8tion.jda.api.entities.channel.ChannelType;
import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.EnumSet;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author :hky
 * @ Date  : Created in 10:22 2022-09-25
 * @ Version discord
 * @ Decribtion : discord机器人，聊天消息$
 */
@Service
@EventBusListenerAnnotation(mode = EventBusMode.ASYNC)
public class DiscordMessageServiceImpl extends ServiceImpl<DiscordMessageMapper, DiscordMessage> implements IDiscordMessageService {

    @Subscribe
    public void syncSaveDiscordMessage(EventBusMessage<Message> messageEvent) {
        if (EventBusUtils.DISCORDMESSAGE.equals(messageEvent.getCode())) {
            DiscordMessage discordMessage = new DiscordMessage();
            Message message = messageEvent.getBody();
            //获取服务器（公会）信息=========================================
            Guild guild = message.getGuild();
            String guildId = guild.getId();
            String guildName = guild.getName();

            //服务器（公会）id
            discordMessage.setGuildId(guildId);
            //服务器（公会）m名称
            discordMessage.setGuiildName(guildName);

            //获取频道信息======================================================
            MessageChannel channel = message.getChannel();
            String channelId = channel.getId();
            String channelName = channel.getName();
            ChannelType channelType = channel.getType();
            //输出频道id
            discordMessage.setChannelId(channelId);
            //输出频道名称
            discordMessage.setChannelName(channelName);
            //输出频道类型
            discordMessage.setChannelType(String.valueOf(channelType.getId()));

            //获取发消息人=========================================================
            Member member = message.getMember();
            String memberId = member.getId();
            String memberNickname = member.getNickname();
            String memberEffectiveName = member.getEffectiveName();
            List<Role> memberRoles = member.getRoles();
            EnumSet<Permission> memberPermissions = member.getPermissions();
            OnlineStatus memberOnlineStatus = member.getOnlineStatus();
            //输出发送消息人的id
            discordMessage.setMemberId(memberId);
            //输出发送消息人的昵称
            discordMessage.setMemberNickname(memberNickname);
            //输出发送消息人的有效名称
            discordMessage.setMemberEffectivename(memberEffectiveName);
            //输出发送消息人的角色列表
            if (CollectionUtils.isNotEmpty(memberRoles)) {
                List<String> collect = memberRoles.stream().map(e -> e.getName()).collect(Collectors.toList());
                discordMessage.setMemberRoles(String.join(",", collect));
            }
            //输出发送消息人的权限列表
            if (CollectionUtils.isNotEmpty(memberPermissions)) {
                List<String> collect = memberPermissions.stream().map(e -> e.getName()).collect(Collectors.toList());
                discordMessage.setMemberPermissions(String.join(",", collect));
            }
            //输出发送消息人的在线状态
            discordMessage.setMemberOnlineStatus(null != memberOnlineStatus ? memberOnlineStatus.getKey() : null);

            //获取消息信息=====================================================================
            String messageId = message.getId();
            String messageContentRaw = message.getContentRaw();
            MessageType messageType = message.getType();
            //输出消息id
            discordMessage.setMessageId(messageId);
            //输出消息内容
            discordMessage.setMessageContentRaw(messageContentRaw);
            //输出消息类型
            discordMessage.setMessageType(null != messageType ? String.valueOf(messageType.getId()) : null);
           //设置discord聊天信息的关键词
            List<String>  sensitiveList = SensitiveWordUtils.dealIk(messageContentRaw);
            if(CollectionUtils.isNotEmpty(sensitiveList)){
                discordMessage.setMessageKeyword(String.join(",",sensitiveList));
            }
            //数据落库
            save(discordMessage);
        }
    }
}
